package com.zany.login.action;

import java.util.Map;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.zany.login.service.LoginService;
import com.zany.system.vo.UserInfoVO;
import com.zany.util.ZanyException;
import com.zany.util.action.AbstractActionSupport;
import com.zany.util.action.MD5;
import com.zany.util.action.SessionInfo;
import com.zany.util.ajax.AjaxTools;

/**
 * 用户登录
 * @author chengY
 * @version 1.0
 */
public class LoginAction extends AbstractActionSupport {
	
	private static final long serialVersionUID = -5032044402458110019L;
	private static Log logger = LogFactory.getLog(LoginAction.class);
	private LoginService loginService;
	private UserInfoVO userInfoVO = new UserInfoVO();
	private Map<String, UserInfoVO> session;
	
	/**
	 * 用户登录
	 * @return
	 * @throws ZanyException 
	 */
	public void login() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("LoginAction.login()");}
//		Properties p = System.getProperties();
//        Enumeration e = p.keys();
//        while (e.hasMoreElements())
//        {
//            String prt = (String) e.nextElement();
//            String prtvalue = System.getProperty(prt);
//            System.out.println(prt + "_____________:" + prtvalue);
//        }
//		System.out.println("___________"+TimeZone.getDefault()); //输出当前默认时区
//		final TimeZone zone = TimeZone.getTimeZone("GMT+8"); //获取中国时区
//		TimeZone.setDefault(zone); //设置时区
//		System.out.println(TimeZone.getDefault()); //输出验证
		
//		System.setProperty("user.timezone","Asia/Shanghai");
		
		UserInfoVO userInfo = null;
		response = ServletActionContext.getResponse();
		if(userInfoVO == null)
			AjaxTools.exAjax("{success:false}", response);
		userInfoVO.setPassword(MD5.md5(userInfoVO.getPassword()));
		userInfo = loginService.login(userInfoVO);
		//将用户基本信息存放到map中
		if(userInfo!=null){
			//SESSION存放用户基本信息
			loginService.updateLastTime(userInfo);
			loginService.updateThisTime(userInfo);
			SessionInfo.setRoleCode(request, userInfo.getRoleCode().toString());
			SessionInfo.setUserCode(request, userInfo.getUserCode());
			SessionInfo.setUserName(request, userInfo.getUserName());
			SessionInfo.setUserOrg(request, userInfo.getUserOrg());
			session.put("userInfo", userInfo);
			AjaxTools.exAjax("{success:true}", response);
		}else{
			AjaxTools.exAjax("{\"success\":false,msg:\"用户名或密码错误！\"}",response);
		}
	}

	public String logout(){
		if(logger.isInfoEnabled()){logger.info("LoginAction.logout()");}
		request.getSession().removeAttribute("USER_CODE");
		request.getSession().removeAttribute("USER_ROLE");
		request.getSession().removeAttribute("USER_NAME");
		request.getSession().removeAttribute("USER_ORG");
		return SUCCESS;
	}
	

	public LoginService getLoginService() {
		return loginService;
	}
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
	public UserInfoVO getUserInfoVO() {
		return userInfoVO;
	}
	public void setUserInfoVO(UserInfoVO userInfoVO) {
		this.userInfoVO = userInfoVO;
	}
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
}
